import 'package:carousel_slider/carousel_controller.dart';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';
import 'package:xvideos/hj_page/mine_business/mine_vip/online_pay_page.dart';
import 'package:xvideos/hj_page/mine_business/mine_vip/vip_card_item.dart';
import 'package:xvideos/hj_page/mine_business/mine_vip/vip_support_model.dart';
import 'package:xvideos/hj_page/pre_sale/pre_sale_provider.dart';
import 'package:xvideos/hj_utils/api_service/mine_service.dart';
import 'package:xvideos/hj_utils/global.dart';
import 'package:xvideos/hj_utils/screen.dart';
import 'package:xvideos/tools_base/base_list_controller.dart';

class MineChargeVipLogic extends BaseListController {
  /// 页面数据
  VipSupportModel? rawDataModel;

  /// 常规会员卡
  VipProductListModel? productModel;

  /// 预售
  VipProductListModel? ysModel;

  /// 当前选中的会员卡
  VipProductModel? currentProductModel;

  /// 跳转选中的vip
  String? vipID;

  /// 会员卡
  late final pageCtr = CarouselController();

  //比例
  double get itemRatio => .448;

  //会员卡高度
  double get cardHeight => (screen.screenWidth * itemRatio) * 216 / 168 + 10;

  @override
  void onReady() {
    super.onReady();
    PreSaleProvider().refreshPreSaleConfig();
    vipID = Get.arguments;
  }

  @override
  instanceChildItem(int index) {
    print('instanceChildItem  $index');
    final model = dataSource[index];
    return VipCardItem(
      model,
      isSelect: currentProductModel == model,
      callback: () {
        pageCtr.animateToPage(index, curve: Curves.fastOutSlowIn);
      },
    );
  }

  @override
  fetchPageData({bool isRefresh = true, bool needLoading = false}) async {
    final res = await MineService.getVipProductInfo();
    isLoading = false;
    rawDataModel = res;
    dataSource.clear();
    rawDataModel?.list?.forEach((element) {
      if (element.position == '会员卡') {
        productModel = element;
      }
      if (element.position == '预售卡') {
        ysModel = element;
      }
    });
    // 优先添加预售卡
    if (ysModel != null &&
        (ysModel?.list ?? []).isNotEmpty &&
        PreSaleProvider().advanceStatus?.activityStatus == true &&
        !PreSaleProvider().isPayAll()) {
      dataSource.add(ysModel!.list!.first..isPreSale = true);
      currentProductModel = dataSource.first;
    }

    dataSource.addAll(productModel?.list ?? []);
    if (vipID != null) {
      currentProductModel = dataSource.firstWhere((element) => element.productID == vipID);
    } else if (currentProductModel == null) currentProductModel = productModel?.list?.first;
    final index = dataSource.indexOf(currentProductModel);
    if (index != -1) {
      Future.delayed(Duration(milliseconds: 100), () => pageCtr.jumpToPage(index));
    }
    update();
  }

  /// 滑动
  onPageChanged(int index) {
    currentProductModel = dataSource[index];
    update();
  }

  onInitiatePayAction() {
    if (currentProductModel == null) {
      showToast("请选择充值的会员卡～");
      return;
    }
    if (currentProductModel!.rchgTypeUI.isEmpty) {
      showToast("未配置支付方式，请联系客服");
      return;
    }
    Get.bottomSheet(
      OnlinePayPage(vipProductModel: currentProductModel),
      isScrollControlled: true,
    );
  }
}
